package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcPaymentCycleMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcPaymentCycle;
import com.csun.cmny.provider.model.domain.OmcReturnFeeConfig;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcPaymentCycleService;
import com.csun.cmny.provider.service.OmcReturnFeeConfigService;
import com.csun.cmny.provider.utils.CronUtil;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.util.PublicUtil;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Transactional
public class OmcPaymentCycleServiceImpl extends BaseService<OmcPaymentCycle> implements OmcPaymentCycleService {

    @Resource
    private OmcPaymentCycleMapper omcPaymentCycleMapper;
    @Resource
    private OmcReturnFeeConfigService omcReturnFeeConfigService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Test
    public void main() {

        // String cronExpression = "0 0 2 25 * ?";
        String cronExpression = "00 00 00 05 1-12 ? *";

//        List<Date> dateList = CronUtil.getNextExecTime(cronExpression, 2);
        Date date = new Date();
        try {
            date = DateUtil.getDate("2019-12-04");
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Date> dateList = CronUtil.getNextExecTimeOfMonthWithDate(cronExpression, date);
        System.out.println("dateList = " + dateList.toString());

        int days = DateUtil.differentDays(dateList.get(0), dateList.get(1));
        System.out.println("days = " + days);

        int month_days = DateUtil.getDaysOfMonth(dateList.get(0));
        System.out.println("month_days = " + month_days);

        boolean flag = CronUtil.isMonthCycle(cronExpression);
        System.out.println("cron is month cycle = " + flag);

        Date last_date = CronUtil.getLastTriggerTime(cronExpression);
        System.out.println("last date = " + last_date);

        Date next_date = CronUtil.getNextTriggerTime(cronExpression);
        System.out.println("next date = " + next_date);
    }

    @Test
    public void test() {

        try {

            Date date = DateUtil.getDate("2019-11-05");

            int month_days = DateUtil.getDaysOfMonth(date);

            System.out.println("date = " + date);
            System.out.println("month_days = " + month_days);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public Integer savePaymentCycle(OmcPaymentCycle omcPaymentCycle, LoginAuthDto loginAuthDto) {

        omcPaymentCycle.setUpdateInfo(loginAuthDto);

        /**
         * @date    2019-09-24
         * @author  chisj
         * @desc    对cron expression进行校验
         */
        String cronExpression = omcPaymentCycle.getPaymentRecordGenerateConfig();
        Preconditions.checkArgument(PublicUtil.isNotEmpty(cronExpression),
                cmnyMessageSource.getMessage("payment.record.generate.config.null", null));

        if (!CronUtil.isValidExpression(cronExpression)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031103);
        }

        // 由于目前周期费用是按月计算，需要校验cronExpression是否按月执行
        if (!CronUtil.isMonthCycle(cronExpression)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031104);
        }

        //校验同一个机构中只有一条配置数据
        //先查询该机构是否有该项配置
        OmcPaymentCycle paymentCycleQuery = new OmcPaymentCycle();
        paymentCycleQuery.setGroupId(omcPaymentCycle.getGroupId());
        paymentCycleQuery = selectOne(paymentCycleQuery);

        //如果存在则修改
        if(paymentCycleQuery != null){
            omcPaymentCycle.setId(paymentCycleQuery.getId());
            return omcPaymentCycleMapper.updateByPrimaryKeySelective(omcPaymentCycle);
            //不存在则插入
        }else{
            return omcPaymentCycleMapper.insertSelective(omcPaymentCycle);
        }
    }

    public boolean checkGroupId(OmcPaymentCycle omcPaymentCycle){
        Example example = new Example(OmcPaymentCycle.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("groupId",omcPaymentCycle.getGroupId());
        if(omcPaymentCycle.getId() != null){
            criteria.andNotEqualTo("id",omcPaymentCycle.getId());
        }
        int ret = selectCountByExample(example);
        return ret<1;
    }
}
